package com.pktechan.api.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pktechan.api.entity.CustCarriage;
import com.pktechan.api.entity.CustCarriageFlow;
import com.pktechan.api.exception.CustomException;
import com.pktechan.api.service.CustCarriageFlowService;
import com.pktechan.api.service.CustCarriageService;
import com.pktechan.api.utils.HtmlUtil;
import com.pktechan.api.vo.CustCarriageFlowVO;
import com.pktechan.api.vo.CustCarriageVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @description:
 * @author: s.zhangbj03307
 * @create: 2021/3/16 11:53
 **/
@RestController
@RequestMapping("custCarriageController")
public class CustCarriageController {

    private static final Logger logger = LoggerFactory.getLogger(CloudPointController.class);

    @Autowired
    private CustCarriageService custCarriageService;
    @Autowired
    private CustCarriageFlowService custCarriageFlowService;

    /**
     * 查看运费流水
     * @param custCarriageFlow
     * @param response
     */
    @RequestMapping(value = "getCustCarriageFlowList",method = RequestMethod.POST)
    public void getCustCarriageFlowList(@RequestBody CustCarriageFlow custCarriageFlow, HttpServletResponse response){
        try {
            logger.info("根据custId="+custCarriageFlow.getCustId()+"查看运费流水");
            PageHelper.startPage(custCarriageFlow.getPageNo(), custCarriageFlow.getPageSize());
            List<CustCarriageFlowVO> list = custCarriageFlowService.getCustCarriageFlowList(custCarriageFlow.getCustId());
            PageInfo pageInfo = new PageInfo(list);
            HtmlUtil.writerSuccessJson(response,pageInfo);
        }catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        }catch (Exception e){
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response,e.getMessage());
        }
    }

    /**
     * 根据custId获取运费兑付流水
     * @param custCarriageFlow
     * @param response
     */
    @RequestMapping(value = "getCarriageWithdrawalFlowList",method = RequestMethod.POST)
    public void getWithdrawalFlowList(@RequestBody CustCarriageFlow custCarriageFlow, HttpServletResponse response){
        try {
            logger.info("根据custId="+custCarriageFlow.getCustId()+"状态获取运费兑付流水");
            PageHelper.startPage(custCarriageFlow.getPageNo(), custCarriageFlow.getPageSize());
            List<CustCarriageFlow> list = custCarriageFlowService.getCarriageWithdrawalFlowList(custCarriageFlow);
            PageInfo pageInfo = new PageInfo(list);
            HtmlUtil.writerSuccessJson(response,pageInfo);
        }catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        }catch (Exception e){
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response,e.getMessage());
        }
    }

    /**
     *  运费提现申请
     * @param custCarriageFlow
     * @param response
     */
    @RequestMapping(value = "freightWithdrawalApply",method = RequestMethod.POST)
    public void freightWithdrawalApply(@RequestBody CustCarriageFlow custCarriageFlow, HttpServletResponse response){
        try {
            logger.info("运费提现申请");
            custCarriageFlowService.freightWithdrawalApply(custCarriageFlow);
            HtmlUtil.writerSuccessJson(response);
        }catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        }catch (Exception e){
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response,e.getMessage());
        }
    }

    /**
     * 运费中心
     * @param custId
     * @param response
     */
    @RequestMapping(value = "getFreightCenter",method = RequestMethod.POST)
    public void getFreightCenter(@RequestParam Integer custId, HttpServletResponse response){
        try {
            logger.info("运费中心");
            CustCarriageVO custCarriageVO = custCarriageService.getFreightCenter(custId);
            HtmlUtil.writerSuccessJson(response,custCarriageVO);
        }catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        }catch (Exception e){
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response,e.getMessage());
        }
    }

    /**
     * 根据运费Id查询运费信息
     * @param carriageId
     * @param response
     */
    @RequestMapping(value = "getCustCarriageById",method = RequestMethod.POST)
    public void getCustCarriageById(@RequestParam Integer carriageId, HttpServletResponse response){
        try {
            logger.info("运费中心");
            CustCarriage custCarriage = custCarriageService.getById(carriageId);
            HtmlUtil.writerSuccessJson(response,custCarriage);
        }catch (Exception e){
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response,e.getMessage());
        }
    }

    /**
     * 根据客户Id查询运费信息
     * @param custId
     * @param response
     */
    @RequestMapping(value = "getCustCarriageByCustId",method = RequestMethod.POST)
    public void getCustCarriageByCustId(@RequestParam Integer custId, HttpServletResponse response){
        try {
            logger.info("运费中心");
            CustCarriage custCarriage = custCarriageService.getByCustId(custId);
            HtmlUtil.writerSuccessJson(response,custCarriage);
        }catch (Exception e){
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response,e.getMessage());
        }
    }
}
